<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px" size="medium" class="ry_form">
      <el-form-item label="价格" prop="price">
        <el-input
          v-model.trim="queryParams.price"
          placeholder="请输入价格"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="连续价格" prop="continuousPrice">
        <el-input
          v-model.trim="queryParams.continuousPrice"
          placeholder="请输入连续价格"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable size="small">
            <el-option
              v-for="dict in dict.type.viptype"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
        </el-select>
      </el-form-item>

      <el-form-item label="原价" prop="originalPrice">
        <el-input
          v-model.trim="queryParams.originalPrice"
          placeholder="请输入原价"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="折扣价" prop="discountPrice">
        <el-input
          v-model.trim="queryParams.discountPrice"
          placeholder="请输入折扣价"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="天数" prop="day">
        <el-input
          v-model.trim="queryParams.day"
          placeholder="请输入天数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item class="flex_one tr">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button :icon="showMoreCondition ? 'el-icon-arrow-up' : 'el-icon-arrow-down'" size="mini" @click="showMoreCondition = !showMoreCondition">{{showMoreCondition ? '收起条件' : '展开条件'}}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['act:FhRechargeData:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['act:FhRechargeData:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['act:FhRechargeData:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="FhRechargeDataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="连续价格" align="center" prop="continuousPrice" />

      <el-table-column label="类型" align="center" prop="type" >
        <template slot-scope="scope">
            <dict-tag :options="dict.type.viptype" :value="scope.row.type"/>
        </template>
      </el-table-column>

      <el-table-column label="原价" align="center" prop="originalPrice" />
      <el-table-column label="折扣价" align="center" prop="discountPrice" />
      <el-table-column label="天数" align="center" prop="day" />
      <el-table-column label="折扣文本" align="center" prop="discountText" />

      <el-table-column label="折扣颜色" align="center" prop="discountColor">
        <template slot-scope="scope">
            <dict-tag :options="dict.type.vip_discount_color" :value="scope.row.discountColor"/>
        </template>
      </el-table-column>

      <el-table-column label="是否选中" align="center" prop="checked">
        <template slot-scope="scope">
            <dict-tag :options="dict.type.vip_checked" :value="scope.row.checked"/>
        </template>
      </el-table-column>

      <el-table-column label="备注" align="center" prop="remark" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['act:FhRechargeData:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['act:FhRechargeData:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改VIP配置表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="108px" inline class="dialog-form-two">

        <el-form-item label="价格" prop="price">
          <el-input v-model.trim="form.price" placeholder="请输入价格" />
        </el-form-item>

        <el-form-item label="连续价格" prop="continuousPrice">
          <el-input v-model.trim="form.continuousPrice" placeholder="请输入连续价格" />
        </el-form-item>

        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
              v-for="dict in dict.type.viptype"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="原价" prop="originalPrice">
          <el-input v-model.trim="form.originalPrice" placeholder="请输入原价" />
        </el-form-item>

        <el-form-item label="折扣价" prop="discountPrice">
          <el-input v-model.trim="form.discountPrice" placeholder="请输入折扣价" />
        </el-form-item>

        <el-form-item label="天数" prop="day">
          <el-input v-model.trim="form.day" placeholder="请输入天数" />
        </el-form-item>

        <el-form-item label="折扣文本" prop="discountText">
          <el-input v-model.trim="form.discountText" placeholder="请输入折扣文本" />
        </el-form-item>
        <el-form-item label="折扣颜色" prop="discountColor">
          <el-select v-model="form.discountColor" placeholder="请选择折扣颜色">
            <el-option
              v-for="dict in dict.type.vip_discount_color"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>


        </el-form-item>

        <el-form-item label="是否选中" prop="checked">

          <el-select v-model="form.checked" placeholder="请选择是否选中">
            <el-option
              v-for="dict in dict.type.vip_checked"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>


        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFhRechargeData, getFhRechargeData, delFhRechargeData, addFhRechargeData, updateFhRechargeData, exportFhRechargeData } from "@/api/act/fhRechargeData";

export default {
  name: "FhRechargeData",
  dicts: ['viptype','vip_checked','vip_discount_color'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // VIP配置表表格数据
      FhRechargeDataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        price: null,
        continuousPrice: null,
        type: null,
        originalPrice: null,
        discountPrice: null,
        day: null,
        discountText: null,
        discountColor: null,
        checked: null,
        loginIp: null,
        loginDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      columns: [
            { key: 1, label: "价格", visible:  true  },
            { key: 2, label: "连续价格", visible:  true  },
            { key: 3, label: "类型", visible:  true  },
            { key: 4, label: "原价", visible:  true  },
            { key: 5, label: "折扣价", visible:  true  },
            { key: 6, label: "天数", visible:  true  },
            { key: 7, label: "折扣文本", visible:  true  },
            { key: 8, label: "折扣颜色", visible:  true  },
            { key: 9, label: "是否选中", visible:  true  },
            { key: 17, label: "备注", visible:  false  },
         ],
      showMoreCondition: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询VIP配置表列表 */
    getList() {
      this.loading = true;
      const {pageNum, pageSize} = this.queryParams;
      const query = {...this.queryParams, pageNum: undefined, pageSize: undefined};
      const pageReq = {page: pageNum - 1, size: pageSize};
      listFhRechargeData(query, pageReq).then(response => {
        const { content, totalElements } = response
        this.FhRechargeDataList = content;
        this.total = totalElements;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        price: null,
        continuousPrice: null,
        type: null,
        originalPrice: null,
        discountPrice: null,
        day: null,
        discountText: null,
        discountColor: null,
        checked: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加VIP配置表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFhRechargeData(id).then(response => {
        this.form = response;
        // 确保数据类型一致，转换为字符串以匹配字典选项
        if (this.form.discountColor !== null && this.form.discountColor !== undefined) {
          this.form.discountColor = String(this.form.discountColor);
        }
        if (this.form.checked !== null && this.form.checked !== undefined) {
          this.form.checked = String(this.form.checked);
        }
        if (this.form.type !== null && this.form.type !== undefined) {
          this.form.type = String(this.form.type);
        }
        this.open = true;
        this.title = "修改VIP配置表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFhRechargeData(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFhRechargeData(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除VIP配置表编号为"' + ids + '"的数据项？').then(function() {
        return delFhRechargeData(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有VIP配置表数据项？').then(() => {
        this.exportLoading = true;
        return exportFhRechargeData(queryParams);
      }).then(response => {
        this.$download.download(response);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
